<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 防伪码管理
 *
 * @icon fa fa-code
 */
class Code extends Backend
{

    /**
     * Code模型对象
     * @var \app\admin\model\Code
     */
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Code;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {


        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $filter_str=$this->request->get("filter");
            $filter_arr = (array)json_decode($filter_str, true);
            $pici_model= new \app\admin\model\Pici;
            $pici=$pici_model->get($filter_arr['pici_id']);
            $this->model->setTable("fw_code_100".$pici->enterp_id);


            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                    ->with(['pici','pro'])
                    ->where($where)
                    ->field("Code.*,Pici.pici_code,Pici.enterp_id as pici_enterp_id,Pro.name,Pro.enterp_id as pro_enterp_id")
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','serial_number','code','pici_id','hits','query_time','query_address','status']);
                $row->visible(['pici']);
				$row->getRelation('pici')->visible(['pici_code']);
				$row->visible(['pro']);
				$row->getRelation('pro')->visible(['name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function qrcode(){
        $pici_id=$this->request->param('pici_id');
        $pici_model= new \app\admin\model\Pici;
        $pici=$pici_model->get($pici_id);
        $this->model->setTable("fw_code_100".$pici->enterp_id);
        $id=$this->request->param('ids');
        $code=$this->model->find($id);
        $url_param="/enterp_code/100".$code->enterp_id;
        $url_param.="/code/".$code->code;
        $url=$this->request->domain()."/qrcode/build?text=".$this->request->domain()."/index/query/query".$url_param;
        echo "<image src='".$url."'>";

    }

}
